/*
 *  Copyright (c) 2022 ZhuHai Jieli Technology Co.,Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*********************************************************************************************
    *   Filename        : p33_port.h

    *   Description     :

    *   Author          : Bingquan

    *   Email           : bingquan_cai@zh-jieli.com

    *   Last modifiled  : 2018-10-31 15:21

    *   Copyright:(c)JIELI  2011-2017  @ , All Rights Reserved.
*********************************************************************************************/

#ifndef _P33_PORT_H_
#define _P33_PORT_H_

#include "typedef.h"
#include "asm/power_interface.h"

#define P33_WKUP_LEVEL_READ()       (JL_P33->RTC_CON & BIT(0))

#define P33_RTC_2Hz_EN(x)           SFR(JL_P33->RTC_CON,  1,  1,  x)
#define P33_RTC_2Hz_READ()          (JL_P33->RTC_CON & BIT(1))
#define P33_RTC_512Hz_EN(x)         SFR(JL_P33->RTC_CON,  2,  1,  x)
#define P33_RTC_512Hz_READ()        (JL_P33->RTC_CON & BIT(2))
#define P33_WKUP_EN(x)              SFR(JL_P33->RTC_CON,  3,  1,  x)
#define P33_WKUP_READ()             (JL_P33->RTC_CON & BIT(3))
#define P33_LEVEL_WKUP_EN(x)        SFR(JL_P33->RTC_CON,  4,  1,  x)
#define P33_LEVEL_WKUP_READ()       (JL_P33->RTC_CON & BIT(4))

#define P33_2Hz_PENDING_READ()      (JL_P33->RTC_CON & BIT(5))
#define P33_512Hz_PENDING_READ()    (JL_P33->RTC_CON & BIT(6))
#define P33_WKUP_PENDING_READ()     (JL_P33->RTC_CON & BIT(7))

#define P33_2Hz_CLEAR_PENDING(x)    SFR(JL_P33->RTC_CON,  13,  1,  x)
#define P33_512Hz_CLEAR_PENDING(x)  SFR(JL_P33->RTC_CON,  14,  1,  x)
#define P33_WKUPCLEAR_PENDING(x)    SFR(JL_P33->RTC_CON,  15,  1,  x)

#define P33_PORT_DECLARE(type)      \
                                    \
void P33_SET_PORT_##type(u8 data);  \
                                    \
u8 P33_GET_PORT_##type(void);       \
                                    \
void P33_AND_PORT_##type(u8 data);  \
                                    \
void P33_OR_PORT_##type(u8 data);   \

P33_PORT_DECLARE(IN)
P33_PORT_DECLARE(OUT)
P33_PORT_DECLARE(DIR)
P33_PORT_DECLARE(DIE)
P33_PORT_DECLARE(PU)
P33_PORT_DECLARE(PD)
P33_PORT_DECLARE(HD)

void P33_DUMP_PORT(void);

#define P33_WKUP_DECLARE(type)      \
                                    \
void P33_SET_WKUP_##type(u8 data);  \
                                    \
u8 P33_GET_WKUP_##type(void);       \
                                    \
void P33_AND_WKUP_##type(u8 data);  \
                                    \
void P33_OR_WKUP_##type(u8 data);   \

P33_WKUP_DECLARE(EN)
P33_WKUP_DECLARE(EDGE)
P33_WKUP_DECLARE(CPND)
P33_WKUP_DECLARE(PND)

void P33_DUMP_WKUP(void);

/****** USART Event *****/
#define JL_WAKEUP_EVENT_PORT_TRIGGER            (1UL << 0)  ///< Port Edge Trigger Wakeup
#define JL_WAKEUP_EVENT_PORT_COUNTER_OVERFLOW   (1UL << 2)  ///< Port Counter overflow wakeup
#define JL_WAKEUP_EVENT_ALARM_ON                (1UL << 3)  ///< Alarm on
#define JL_WAKEUP_EVENT_CHAGRE_IN               (1UL << 4)  ///< Charger Insert
#define JL_WAKEUP_EVENT_LOW_VOLTAGE             (1UL << 5)  ///< System Low Volatage

typedef void (*JL_WAKEUP_SignalEvent_t)(uint32_t event); // Pointer to \ref JL_WAKEUP_SignalEvent : Signal Wakeup Event.

struct port_wakeup_driver {
    u8 pullup_down_enable;
    u8 edge;        // [0]: Rising / [1]: Falling
    u8 attribute;   // Relate operation bitmap OS_RESUME | BLUETOOTH_RESUME
    u8 iomap;       // Port Group - Port Index
    u8 filter_enable;
};

struct charge_wakeup_driver {
    u8 attribute;   // Relate operation bitmap OS_RESUME | BLUETOOTH_RESUME
};

struct alarm_wakeup_driver {
    u8 attribute;   // Relate operation bitmap OS_RESUME | BLUETOOTH_RESUME
};

struct lvd_wakeup_driver {
    u8 attribute;   // Relate operation bitmap OS_RESUME | BLUETOOTH_RESUME
};

struct sub_wakeup_driver {
    u8 attribute;   // Relate operation bitmap OS_RESUME | BLUETOOTH_RESUME
};

// <Max hardware wakeup port
#define MAX_WAKEUP_PORT     8
#define MAX_RTC_PORT         2

struct wakeup_driver_hdl {
    struct port_wakeup_driver       port[MAX_WAKEUP_PORT];
    struct port_wakeup_driver         rtc_port[MAX_WAKEUP_PORT];
    struct charge_wakeup_driver     charge;
    struct alarm_wakeup_driver      alram;
    struct lvd_wakeup_driver        lvd;
    struct sub_wakeup_driver        sub;
    JL_WAKEUP_SignalEvent_t         cb_event;      // Event callback
};

u8 power_wakeup_exit(void);
void __hw_wakeup_port_init(const struct wakeup_param *param);
void charge_set_callback(void (*wakup_callback)(void), void (*sub_callback)(void));
void chargestore_set_callback(void (*callback)(void));
const struct wakeup_param *power_get_wakeup_param(void);

#endif

